<?php
/**
 * +----------------------------------------------------------------------
 * | summer framework
 * +----------------------------------------------------------------------
 * | Copyright (c) 2020 https://github.com/suframe/think-admin All rights reserved.
 * +----------------------------------------------------------------------
 * | Author: summer <806115620@qq.com>  2020/4/16 13:55
 * +----------------------------------------------------------------------
 */

namespace app\mall\enum;

use \app\mall\enum\OrderAdminOpsEnum as adminOps;
use \app\mall\enum\OrderUserOpsEnum as usernOps;

class OrderStatusEnum extends Base
{

    public const NEW = 1;
    public const CANCEL = 2;
    public const REFUND = 3;
    public const PAID = 4;
    public const SHIPPED = 5;
    public const SERVED = 6;
    public const COMPLETED = 9;

    public static function toZhArray(): array
    {
        return [
            self::NEW => '未支付',
            self::PAID => '已支付',
            self::SHIPPED => '已发货',
            self::SERVED => '已收货',
            self::COMPLETED => '已完成',
            self::CANCEL => '已取消',
            self::REFUND => '已退款',
        ];
    }

    public function canRefund()
    {
        return in_array($this->getValue(), [
            static::PAID,
            static::SHIPPED,
            static::SERVED,
        ]);
    }

    public function canCancel()
    {
        return $this->getValue() == static::NEW;
    }

    public function canSend()
    {
        return $this->getValue() == static::PAID;
    }

    public function canCompleted()
    {
        return in_array($this->getValue(), [
            static::PAID,
            static::SHIPPED,
            static::SERVED,
        ]);
    }

    public function canEvaluate()
    {
        return in_array($this->getValue(), [
            static::COMPLETED,
        ]);
    }

    public function getAdminOps()
    {
        $ops = [];
        switch ($this->getValue()) {
            case static::NEW:
                $ops[] = adminOps::CANCEL;
                break;
            case static::PAID:
                $ops[] = adminOps::SEND;
                break;
            case static::SHIPPED:
                $ops[] = adminOps::COMPLETE;
                break;
            case static::COMPLETED:
                $ops[] = adminOps::EVALUATE;
                break;
        }
        $rs = [];
        foreach ($ops as $op) {
            $rs[] = ['op' => $op, 'label' => (new adminOps($op))->getZhName()];
        }
        return $rs;
    }

    public function getClerkOps()
    {
        return $this->getAdminOps();
    }

    public function getUserOps()
    {
        $ops = [];
        switch ($this->getValue()) {
            case static::NEW:
                $ops[] = usernOps::CANCEL;
                break;
            case static::PAID:
                $ops[] = usernOps::REFUND;
                break;
            case static::SHIPPED:
                $ops[] = usernOps::REFUND;
                $ops[] = usernOps::COMPLETE;
                break;
            case static::COMPLETED:
                $ops[] = usernOps::EVALUATE;
                break;
        }
        $rs = [];
        foreach ($ops as $op) {
            $rs[] = ['op' => $op, 'label' => (new usernOps($op))->getZhName()];
        }
        return $rs;
    }

}